<?php

class Empeval_Methods_Request_SetBonusGoalsFromData {
	
	/**
	 *
	 * @var App_Methods_Data_App
	 */
	public $app;
	public $bonusGoalsFormId;
	public $values;
	
	public function __construct($params) {
		$this->app = new App_Methods_Data_App($params->app);
		$this->bonusGoalsFormId = $params->bonusGoalsFormId;
		$this->values = $params->getDefault('values');
	}
	
	public function process() {
		$bonusGoalsForm = EmpevalBonusGoalsFormsQuery::create()
		->filterById($this->bonusGoalsFormId)
		->filterByEditorId(Empeval_Auth::getInstance()->getUser()->getId())
		->findOne();
		
		if (!($bonusGoalsForm instanceof EmpevalBonusGoalsForms))
			throw new Empeval_Exception_App(Empeval_Exception_Codes::INFO_ACCESS_DENIED);
		
		$con = Propel::getConnection('vgp_empeval');
		$con->beginTransaction();
		try {
			foreach ($this->values as $value) {
				$bonusGoalsForm->setByName($value['field'], $value['value'], BasePeer::TYPE_STUDLYPHPNAME);
			}
			$bonusGoalsForm->save($con);
			$con->commit();
		} catch (Exception $e) {
			$con->rollBack();
			throw $e;
		}
		
		return new Empeval_Methods_Response_SetBonusGoalsFromData(true);
	}
	
}